package com.tanhua.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.domain.UserStatus;
import com.tanhua.mapper.UserStatusMapper;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;


@Configuration //用于标记配置类
@EnableScheduling //开启定时任务
public class UserStatusJob {

    @Autowired
    private UserStatusMapper userStatusMapper;

    @Reference
    private UserInfoApi userInfoApi;

    @Scheduled(cron = "0 0/5 * * * ?")
    private void job(){
        System.out.println("查看是否有需要解封的账户");
        QueryWrapper<UserStatus> wrapper = new QueryWrapper<>();
        wrapper.lt("un_freeze_time",new Date());
        List<UserStatus> userStatusList = userStatusMapper.selectList(wrapper);
        if(userStatusList==null){
            return;
        }
        for (UserStatus userStatus : userStatusList) {
            UserInfo userInfo = userInfoApi.findById(userStatus.getUserId().longValue());
            userInfo.setUserStatus("1");
            userInfoApi.update(userInfo);
            userStatusMapper.deleteById(userStatus.getId());
        }
    }
}